package mp

import (
	"context"

	"ruikc.com/server/api/internal/db"
	"ruikc.com/server/api/internal/svc"
	"ruikc.com/server/api/internal/types"
	"ruikc.com/server/models"
	xb "xorm.io/builder"

	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
)

type InstructorListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewInstructorListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InstructorListLogic {
	return &InstructorListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *InstructorListLogic) InstructorList(req *types.InstructorSearch) (resp *types.InstructorList, err error) {
	var datas []models.Instructor
	conds := xb.And(xb.Gt{"id": 0})
	if req.Keyword != "" {
		conds = conds.And(xb.Like{"title", "'%" + req.Keyword + "%'"})
	}
	db.FindPageDatas(&datas, conds, req.Page)
	total := db.CountData(&models.Instructor{}, conds)
	items := []types.Instructor{}
	for _, v := range datas {
		tmp := types.Instructor{}
		copier.Copy(&tmp, &v)
		items = append(items, tmp)
	}
	return &types.InstructorList{
		Total: int(total),
		Items: items,
	}, nil

}
